package com.sky.proxy.cglibProxy1;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

/**
 * @author : wushikai
 * <p>
 * date : 2022-03-10
 */
public class CGLibProxyMain {

    public static void main(String[] args) {
        DBQueryProxy dbQueryProxy = new DBQueryProxy();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(DBQuery.class);
        enhancer.setCallback(dbQueryProxy);
//        DBQuery dbQuery = (DBQuery)enhancer.create(new Class[]{Integer.class}, new Object[]{1});


        DBQuery dbQuery = (DBQuery) enhancer.create();
        System.out.println(dbQuery.getElement("Hello"));
        System.out.println();
        System.out.println(dbQuery.getAllElements());
        System.out.println();
        System.out.println(dbQuery.sayHello());




        DBQueryProxy dbQueryProxy1 = new DBQueryProxy();
        DBQueryProxy2 dbQueryProxy2 = new DBQueryProxy2();
        Enhancer enhancer1 = new Enhancer();
        enhancer1.setSuperclass(DBQuery.class);
        enhancer1.setCallbacks(new Callback[]{dbQueryProxy1, dbQueryProxy2});
        enhancer1.setCallbackFilter(new CallbackFilter() {

            public int accept(Method method) {
                if (method.getName().equals("getElement")) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        DBQuery dbQuery1 = (DBQuery) enhancer1.create();
        System.out.println("========Inteceptor By DBQueryProxy ========");
        System.out.println(dbQuery1.getElement("Hello"));
        System.out.println();
        System.out.println("========Inteceptor By DBQueryProxy2 ========");
        System.out.println(dbQuery1.getAllElements());






        Object  obj  =  new String[] {"123" , "1234"};

        System.out.println(obj);
        obj  = new int[] {1,2,3} ;

        System.out.println(obj);
        obj  = new float[]{ 1F , 2F} ;
        System.out.println(obj);




    }
}
